package org.springblade.modules.teambition.config;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.utils.DateUtil;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

@Slf4j
public class UtcDateDeserializer extends JsonDeserializer<String> {

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

	@Override
	public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
		String date = p.getText();
		log.info("source{}",date);
		try {
			sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
			Date parse = sdf.parse(date);
			String format = DateUtil.format(parse, DateUtil.PATTERN_DATETIME);
			log.info("target{}",format);
			return format;
		} catch (ParseException e) {
			return "";
		}
	}
}
